package com.educate.common.utils;

import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class App {

    public static void main(String[] args) throws Exception {
        test2();
    }

    public static void test2() throws Exception {
        List<Float> result = new ArrayList<>(1001);

        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec("python tensorflow/examples/label_image/label_image_service.py", null,
                new File("/root/tensorflow-1.4.1"));
        if (proc != null) {
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            try {
                String line;
                while ((line = in.readLine()) != null) {
                    String[] split = line
                            .replace("[", "")
                            .replace("]", "").split(" ");
                    for (String item : split) {
                        item = item.trim();
                        if (item.length() == 0) {
                            continue;
                        }
                        BigDecimal bd = new BigDecimal(item);
                        float floatValue = bd.floatValue();
                        result.add(floatValue);
                        System.out.println(floatValue);
                    }
                }
                proc.waitFor();
                in.close();
//                out.close();
                proc.destroy();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("result.size() = " + result.size());
    }

    public static void executeNewFlow() {
        Runtime run = Runtime.getRuntime();
        File wd = new File("/bin");
        System.out.println(wd);
        Process proc = null;
        try {
            proc = run.exec("/bin/bash", null, wd);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (proc != null) {
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
            out.println("cd /home/test");
            out.println("pwd");
            out.println("rm -fr /home/proxy.log");
            out.println("exit");
            try {
                String line;
                while ((line = in.readLine()) != null) {
                    System.out.println(line);
                }
                proc.waitFor();
                in.close();
                out.close();
                proc.destroy();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
